function axiosRequest(method, url, data) {
	let result = axios({
		method,
		url: 'http://121.37.227.40:8000' + url,
		data,
		// header: {
		// 	'Content-Type': 'application/x-www-form-urlencoded'
		// }
	})
	return result;
}
function axiosRequest1(method, url, data) {
	let result = axios({
		method,
		url: 'http://121.37.227.40:8000/system' + url,
		data,
		// header: {
		// 	'Content-Type': 'application/x-www-form-urlencoded'
		// }
	})
	return result;
}
function axiosJson(method, url, serializeArray, dataObject) {
	if (serializeArray==null) {
		serializeArray = []
	}
	if (dataObject==null) {
		dataObject = {}
	}
	for (let i = 0; i < serializeArray.length; i++) {
		dataObject[serializeArray[i].name] = serializeArray[i].value
	}

	return axios({
		method: method,
		url: 'http://121.37.227.40:8000' + url,
		params: dataObject
	})
}

function axiosBody(url, serializeArray, objArr) {
    let dataObject = {}
    if (serializeArray == null) {
        serializeArray = []
    }
    for (let i = 0; i < serializeArray.length; i++) {
        dataObject[serializeArray[i].name] = serializeArray[i].value
    }
    console.log(dataObject)
    console.log(objArr)
    return axios({
        method: 'post',
        url: 'http://121.37.227.40:8000' + url,
        params: dataObject,
        data: objArr
    })
}

function addressMenuInit(province, city) {
	city = city[0];
	var url="http://121.37.227.40:8000/"
	axios({
		url:url+'marketing/queryCity',
		method:"POST",
		dataType:"JSON"
	}).then(function (rel){
		rel=rel.data;
		let str='<option value="0">请选择省份</option>';
		let s="";
		var arrs= [];
		arrs[0] = [" ","请选择城市"];
		for (var i=0;i<rel.length;i++){
			if (rel[i].province.pName===s){
				arrs[rel[i].province.id].push(rel[i].cName) ;
			}else {
				str+='<option value="'+rel[i].province.id+'">'+ rel[i].province.pName+'</option>';
				arrs[rel[i].province.id] = [rel[i].province.pName,"请选择城市",rel[i].cName];
				s=rel[i].province.pName;
			}
		}
		province.html(str)

		province.change(function (){
			remove1(city);
			let val=$("#country").val()
			for(var i = 1; i < arrs[val].length; i++) {
				var option1 = document.createElement("option");
				var text1 = document.createTextNode(arrs[val][i]);
				for (var j=0;j<rel.length;j++) {
					if (arrs[val][i]===rel[j].cName){
						option1.value =""+rel[j].id;
					}else {
						continue;
					}
				}
				option1.appendChild(text1);
				city.appendChild(option1);
			}
		})
	})
}

function remove1(citysel) {
	var options = citysel.getElementsByTagName("option");
	for(var i = 0; i < options.length; i++) {
		citysel.removeChild(options[i]);
		i--;
	}
}


/**
 * 职位、姓名下拉框功能
 * @param post 职位下拉框jq对象
 * @param name 姓名下拉框jq对象
 */
function assignmentMenuInit(post, name) {
	name = name[0];
	var url="http://121.37.227.40:8000/"
	axios({
		url:url+'marketing/queryName',
		method:"POST",
		dataType:"JSON"
	}).then(function (rel){
		rel=rel.data;
		let str='<option value="0">请选择</option>';
		let s="";
		var arrs= [];
		arrs[0] = [" ","请选择"];
		for (var i=0;i<rel.length;i++){
			if (rel[i].job.jobName===s){
				arrs[rel[i].job.jobId].push(rel[i].uName) ;
			}else {
				str+='<option value="'+rel[i].job.jobId+'">'+ rel[i].job.jobName+'</option>';
				arrs[rel[i].job.jobId] = [rel[i].job.jobName,"请选择",rel[i].uName];
				s=rel[i].job.jobName;
			}
		}
		post.html(str)

		post.change(function (){
			remove1(name);
			let val=$("#post").val()
			for(var i = 1; i < arrs[val].length; i++) {
				var option1 = document.createElement("option");
				var text1 = document.createTextNode(arrs[val][i]);
				for (var j=0;j<rel.length;j++) {
					if (arrs[val][i]===rel[j].uName){
						option1.value =""+rel[j].uId;
					}else {
						continue;
					}
				}
				option1.appendChild(text1);
				name.appendChild(option1);
			}
		})
	})
}
/**
 * 省市下拉框逻辑带默认值
 * @param province 省下拉框jq对象
 * @param city 市下拉框jq对象
 * @param provinceId 默认选中的省id
 * @param cityId 默认选中的市id
 */
function addressMenuInitOfDefault(province, city, provinceId, cityId) {
	if (provinceId==null) provinceId=0;
	if (cityId==null) cityId=0;
	axiosRequest("get", "/system/provinceList", null).then((res)=>{
		let provinceList = res.data;
		let cityMap = [];
		axiosRequest("get", "/system/cityMap", null).then((res)=>{
			cityMap = res.data;
			cityMap[0] = []
			province.change(()=>{
				let html = `<option value="0" `+(cityId==0 ? `selected` : ` `)+`>请选择城市</option>`
				let cityList = cityMap[province.val()];
				for (let i = 0; i < cityList.length; i++) {
					html += `<option value="`+cityList[i].id+`" `+(cityId==cityList[i].id ? `selected` : ` `)+`>`+cityList[i].cName+`</option>`
				}
				city.html(html);
			})
			province.change();
		})

		let html = `<option value="0" `+(provinceId==0 ? `selected` : ` `)+`>请选择省份</option>`;
		for (let i = 0; i < provinceList.length; i++) {
			html += `<option value="`+provinceList[i].id+`" `+(provinceId==provinceList[i].id ? `selected` : ` `)+`>`+provinceList[i].pName+`</option>`
		}
		province.html(html);

	})
}

//根据传递过来的参数name获取对应的值
function getParameter(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = location.search.substr(1).match(reg);
    if (r != null) return (r[2]);
    return null;
}

//全选/全不选
function selectAll() {
    $('#checkAll').unbind('click')
    $("[name='check']").unbind('click')
    //点击全选复选框、触发事件
    $('#checkAll').click(function () {
        //判断全选复选框、有没有被选中
        if ($(this).is(":checked")) {
            //若被选中、则将[name='check'] 的复选框、依次选上
            $("[name='check']").each(function () {
                $(this).attr("checked", true)
            })
        } else {
            //若全选按钮取消选中，则[name='check'] 的复选框都会取消选中
            $("[name='check']").each(function () {
                $(this).attr("checked", false)
            })
        }
    })
    //只要下面的复选框有一个没选上，全选复选框则取消选中
    $("[name='check']").click(function () {
        var flag = true;
        $("[name='check']").each(function () {
            if (!$(this).is(":checked")) {
                flag = false
            }
        })
        $('#checkAll').attr("checked", flag);
    })
}

function addressMenuInit1(province, city,pname) {
	city = city[0];
	var url="http://121.37.227.40:8000/"
	axios({
		url:url+'marketing/queryCity',
		method:"POST",
		dataType:"JSON"
	}).then(function (rel){
		rel=rel.data;
		console.log(rel)
		let str='<option value="0">请选择省份</option>';
		let s="";
		var arrs= [];
		arrs[0] = [" ","请选择城市"];
		for (var i=0;i<rel.length;i++){
			if (rel[i].province.pName===s){
				arrs[rel[i].province.id].push(rel[i].cName) ;
			} else {
				if (pname==rel[i].province.pName){
					str+='<option value="'+rel[i].province.id+'" selected>'+ rel[i].province.pName+'</option>';
					arrs[rel[i].province.id] = [rel[i].province.pName,"请选择城市",rel[i].cName];
					s=rel[i].province.pName;
					continue;
				}
				str+='<option value="'+rel[i].province.id+'">'+ rel[i].province.pName+'</option>';
				arrs[rel[i].province.id] = [rel[i].province.pName,"请选择城市",rel[i].cName];
				s=rel[i].province.pName;
			}
		}
		province.html(str)

		province.change(function (){
			remove1(city);
			let val=$("#country").val()
			for(var i = 1; i < arrs[val].length; i++) {
				var option1 = document.createElement("option");
				var text1 = document.createTextNode(arrs[val][i]);
				for (var j=0;j<rel.length;j++) {
					if (arrs[val][i]===rel[j].cName){
						option1.value =""+rel[j].id;
					}else {
						continue;
					}
				}
				option1.appendChild(text1);
				city.appendChild(option1);
			}
		})
	})
}